class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        const int n = nums.size();
        vector<int> after(n);
        after[n - 1] = 1;
        for(int i = n - 2; i >= 0; --i)
            after[i] = after[i + 1] * nums[i + 1];

        int tmp = 1;
        for(int i = 1; i < n; ++i)
        {
            tmp *= nums[i - 1];
            after[i] *= tmp;
        }
        return after;
    }
};
